<template>
    <div class="handleDetail">
        <el-menu
            style="
                width: 100px;
                position: fixed;
                top: 0;
                left: 0;
                height: 100vh;
            "
            default-active="0"
        >
        <template>
            <el-menu-item

                v-for="(comName2, index) in comArr[activeComNumber].comName"
                :key="comName2"
                :index="index + ''"
            >
                <span slot="title">
                    <div
                    v-if="!(comName2 == '审核意见' && isLook)"
                        @click="
                            menuClick(comArr[activeComNumber].comName[index])
                        "
                    >
                        {{ comArr[activeComNumber].label[index] }}
                    </div>
                </span>
            </el-menu-item>
        </template>

        </el-menu>
        <div
            style="
                width: calc(100vw - 140px);
                margin-left: 100px;
                margin-bottom: 37px;
                height: calc(100% - 42px);
                overflow: auto;
            "
        >
            <div
                v-for="(elTab, index) in comArr[activeComNumber].label"
                :label="elTab"
                :key="elTab"
            >
                <div :id="comArr[activeComNumber].comName[index]"></div>
                <component
                    :ADDEDITLOOK="ADDEDITLOOK"
                    :isLook="isLook"
                    :nodeId="nodeId"
                    :busType="busType"
                    :flag="comArr[activeComNumber].flag"
                    :is="comArr[activeComNumber].comName[index]"
                    :ref="comArr[activeComNumber].comName[index]"
                ></component>
            </div>
        </div>
        <div class="btnList">
            <template v-if="isLook">
                <el-button
                    style="position: relative;z-index: 1000;"
                    @click="btnFn('取消')"
                    size="small"
                >
                    取消
                </el-button>
            </template>
            <template v-else>
                <el-button
                    style="position: relative;z-index: 1000;"
                    v-for="btnName in comArr[activeComNumber].btnArray"
                    @click="btnFn(btnName)"
                    :key="btnName"
                    size="small"
                    :type="btnName == '取消' ? '' : 'primary'"
                >
                    {{ btnName }}
                </el-button>
            </template>
        </div>
        <vxe-modal v-model="msgShow" width="60%"  @close="msgDialogFn">
                <template v-slot>
                    <div style="max-height: 100vh;overflow-y: auto;overflow-x: hidden;">
                        <div v-for="(msg,index) in messageDialog" :key="index">
                            {{ msg }}
                        </div>
                    </div>

                </template>
          </vxe-modal>

          <!-- <errorDialog v-model="dialogVisible" :contentArray="contentArray"></errorDialog> -->
    </div>
</template>

  <script>
import com from '../com.json'
import sqxx from '../flowNew/StartProcess1.vue'
import fpxx from '../flowNew/InvoiceInfo2.vue'
import shgj from '../flowNew/ProcessTrack4.vue'
import jbxx from '../flowNew/BaseInfo5.vue'
import shyj from '../flowNew/ProcessAdvice6.vue'
import hzqrdsq from '../flowNew/redInvoiceApply7.vue' // 4.7 红字确认单申请
import hzqrdsp from '../flowNew/redInvoiceApprove8.vue' // 4.7 红字确认单审批
import hzqrdsqbc from '../flowNew/redInvoiceApplySave7.vue' // 4.7 单纯保存

import hzqrdqr from '../flowNew/redInvoiceConfig9.vue' // 4.8 红字确认单确认
import hzqrdqrsh from '../flowNew/redInvoiceConfig10.vue' // 4.8 红字确认单审核
import hzqrd from '../flowNew/redInvoiceResult11.vue'
import { saveOrSubmit ,handlingProcess, stopProcess} from '@/api/businessFlow.js'
// import errorDialog from '@/components/errorDialog/indexComponents.vue'

import { beforeSaveValid } from './beforeSaveValids.js'


export default {
    name: 'handleDetail',
    components: {
        fpxx,
        sqxx,
        shgj,
        jbxx,
        shyj,
        hzqrdsq,
        hzqrdsqbc,
        hzqrdsp,
        hzqrd,
        hzqrdqr,
        hzqrdqrsh,

        // errorDialog,
    },
    props:{
        ADDEDITLOOK:{
            type: String,
            default:'EDIT'
        }
    },
    data() {
        return {
            // dialogVisible: false,

            // contentArray:[
            //     // {
            //     //     lab: "申请成功！",
            //     //     val:''
            //     // },
            //     // {
            //     //     lab: "红字确认单编号：",
            //     //     val:'695412541584955846512'
            //     // },
            //     // {
            //     //     lab: "红方确认单状态：",
            //     //     val:'销方录入待购方确认'
            //     // },
            // ],
            messageDialog:[],
            msgShow:false,

            comArr: com,
            activeComNumber: '0',
            processId: '',
            nodeId: '',
            busId: '',
            busType: '',
            templateId: '',
            procInstId: '',
            isLook: false,
        }
    },
    created() {
        let cache = sessionStorage.getItem('handleDetailInfo')
        if (cache) {
            cache = JSON.parse(cache);
            console.log(cache,'cache------------');
            this.activeComNumber = cache.activeComNumber
            this.processId  = cache.processId

            this.nodeId  = cache.nodeId // currentNodeName

            this.busId  = cache.busId
            this.busType  = cache.busType

            this.templateId  = cache.templateId
            this.procInstId  = cache.procInstId
            this.isLook  = cache.isLook
        }
    },
    methods: {
        msgDialogFn(){
            this.msgShow = false;
            this.messageDialog = []
        },
        closeFather() {
            window.parent.postMessage(
                {
                    type: '02',
                    params: {
                        url: '/handleDetail',
                        width: 1000,
                        height: 800,
                        title: '测试hello',
                        windowId: 'handleDetail',
                    },
                },
                '*'
            )
        },
        menuClick(dom) {
            document.getElementById(dom).scrollIntoView({ behavior: 'smooth' })
            return false
        },
        cancelConfirm(){
            if(this.ADDEDITLOOK == 'ADD' && !this.processId ){ // 新增分为 新增和启动流程,流程有id需要排除
                if(this.busType == 'FLOW_SGKP'){
                    return this.$confirm( `是否放弃本次新增开票申请？`,'确认').then(data=>{
                         this.closeDialog()
                    },err=>{
                    })
                }else if(this.busType == 'FLOW_HZQRDSQ'){
                    return this.$confirm( `是否放弃本次红字确认单申请？`,'确认').then(data=>{
                         this.closeDialog()
                    },err=>{
                    })
                }
                this.closeDialog()

            }else{
                this.closeDialog()
            }
        },
        async btnFn(name) {
            console.log(this);
            if (name === '取消') {
                this.cancelConfirm()
            } else  if (name === '保存') {
                 let isContinue =  await  beforeSaveValid(this.busType,this)


                 isContinue&& this.save();
            } else if (name === '提交') {
                let isContinue =  await  beforeSaveValid(this.busType,this)
                 console.log(isContinue);

                 isContinue&& this.submit();
            } else if (name === '确认') {
                this.confirm()
            } else if (name === '终止') {
                this.stop()
            } else if (name === '开票') {
                this.invoice(this.INVOICERECEIVE.INVOICE);
            } else if (name === '下载授信额度') {
                this.invoice(this.INVOICERECEIVE.DOWNLIMIT);
            } 
            else if (name === '退回授信额度') {
                this.invoice(this.INVOICERECEIVE.RETURNLIMIT)
            } 
            else if (name === '领用') {
                this.invoice(this.INVOICERECEIVE.RECEIVE);
            }
        },
        async confirm(){ // 确认

                let params = JSON.parse(
                    sessionStorage.getItem('handleDetailInfo')
                )
                let obj = {
                    "busId": this.busId,
                    // "busId": params.busId,
                    "handleResult": this.$store.state.invoice2.invoinceFormAdvice6.handleResult,
                    "nextHandleUser": this.$store.state.invoice2.invoinceFormAdvice6.nextHandleUser,
                    "remark": this.$store.state.invoice2.invoinceFormAdvice6.remark
                }
            // if(!this.$store.state.invoice2.invoinceFormAdvice6.nextHandleUser)return this.$message.error('请选择即将流向人')
            if(this.$store.state.invoice2.invoinceFormAdvice6.handleResult == 3&&!this.$store.state.invoice2.invoinceFormAdvice6.remark)return this.$message.error('请填写事项说明')
            let { data, code, message } = await handlingProcess(
                obj
            )
            if(code ==200){
                this.$message.success(message || data)
                this.closeDialog()
            }else{
                // this.dialogVisible = true;
                // this.dialogVisible = [
                //     {
                //         lab: data
                //     }
                //     // {
                //     //     lab: "申请成功！",
                //     //     val:''
                //     // },
                //     // {
                //     //     lab: "红字确认单编号：",
                //     //     val:'695412541584955846512'
                //     // },
                //     // {
                //     //     lab: "红方确认单状态：",
                //     //     val:'销方录入待购方确认'
                //     // },
                // ]
            }
        },
        async invoice(ResultMsg){ // 开票
            let params = JSON.parse(
                    sessionStorage.getItem('handleDetailInfo')
                )
            let  obj = {
                    "handleResult": 1,
                    "busId": this.busId,
                    // "busId": params.busId,
                }

            let { data, code, message } = await handlingProcess(
                obj
            )
            if(code == 200){
                let msg = '处理完毕，请在【已开发票管理】中进行发票上传操作！';
                if(ResultMsg == this.INVOICERECEIVE.INVOICE){
                    msg = '处理完毕，请在【已开发票管理】中进行发票上传操作！'
                }else if(ResultMsg == this.INVOICERECEIVE.RECEIVE){
                    msg = '领用成功！'
                }
                else if(ResultMsg == this.INVOICERECEIVE.DOWNLIMIT){
                    msg = '下载授信额度成功！'
                } else if(ResultMsg == this.INVOICERECEIVE.RETURNLIMIT){
                    msg = '退回授信额度成功！'
                }
                this.$alert(msg, '提示', {
                    confirmButtonText: '确认',
                    callback: action => {
                        this.closeDialog()
                    }
                    }).then(data=>{
                    },err=>{
                    })

            }
        },
        // 终止
        async stop(){
            let params = JSON.parse(
                    sessionStorage.getItem('handleDetailInfo')
                )
            let  obj = {
                    "busId": this.busId
                    // "busId": params.busId
                }

            let { data, code, message } = await stopProcess(
                obj
            )
            if(code ==200){
                this.$message.success(data || message)
                this.closeDialog()
            }
        },
        // 关闭弹框
        closeDialog(){
            this.$emit('closeSearch')
                try {
                    this.closeFather();

                } catch(e) {

                }
        },

        // 保存
        async save(){
            this.loading = true;
            let params = JSON.parse(
                    sessionStorage.getItem('handleDetailInfo')
                )
            let obj = {
                "approvalUser": this.$store.state.invoice2.invoinceFormStart1.approvalUser,
                "busData": JSON.stringify(this.$store.state.invoice2.busData),
                "busId": this.busId || this.$store.state.invoice2.invoinceFormStart1.busId,
                // "busId": params.busId || this.$store.state.invoice2.invoinceFormStart1.busId,
                "busType": this.busType,
                "createTime": this.$store.state.invoice2.invoinceFormStart1.createTime,
                "description": this.$store.state.invoice2.invoinceFormStart1.description,
                "submit": false
            }



            if(!obj.approvalUser)return this.$message.error('请选择即将流向人')
            let { data, code, message } = await saveOrSubmit(
                obj
            )
            if (code == 200) {
                this.closeDialog();
                this.$message.success(message || data)
            }else if(code === '7012'){
                this.msgShow = true;
                        this.messageDialog = data
            }
        },
        // 提交
        async submit(){
            this.loading = true;
            // let params = JSON.parse(
            //         sessionStorage.getItem('handleDetailInfo')
            //     )
            let obj = {
                "approvalUser": this.$store.state.invoice2.invoinceFormStart1.approvalUser,
                "busData": JSON.stringify(this.$store.state.invoice2.busData)  ,
                "busId": this.busId || this.$store.state.invoice2.invoinceFormStart1.busId,
                // "busId": params.busId || this.$store.state.invoice2.invoinceFormStart1.busId,
                "busType": this.busType,
                "createTime": this.$store.state.invoice2.invoinceFormStart1.createTime,
                "description": this.$store.state.invoice2.invoinceFormStart1.description,
                "submit": true
            }

            if(!obj.approvalUser)return this.$message.error('请选择即将流向人')


            let { data, code, message } = await saveOrSubmit(
                obj
            )
            if (code == 200) {
                this.$message.success(message || data)
                this.closeDialog();
            }else if(code === '7012'){
                this.msgShow = true;
                this.messageDialog = data
            }
        }
    },
}
</script>

  <style scoped lang = "scss">
.handleDetail {
    overflow-y:  auto;
    overflow-x: hidden;
    // height: 100%;
    .btnList {
        display: flex;
        justify-content: center;
        position: fixed;
        z-index: 3;
        bottom: 5px;
        left: 0;
        right: 0;
        height: 32px;
    }
}
</style>
